<?php
/**
 * 货品列表控制器
 * @author e5
 *
 */
class GoodsListController extends AuthController{
	private $db;
	public function __init(){
		parent::__init();
		$this->db = K("GoodsList");
	}
	/**
	 * 货品列表
	 */
	public function index(){
		$gid = Q("get.gid",0,"intval");
		//处理分页
		$total = $this->db->where("vshop_goods_gid={$gid}")->count();
		$page = new Page($total,6);
		$pageData = "";
		if ($total > 6){
			$pageData = $page->show();
		}
		//获得商品属性
		$tid = K("Goods")->field("vshop_type_tid")->where("gid={$gid}")->find();
		$attrsData = K("TypeAttr")->where("vshop_type_tid={$tid['vshop_type_tid']} AND tatype=1")->all();
		$this->assign("attrsData",$attrsData);
		$goodsAttrs = K("GoodsAttr")->where("vshop_goods_gid={$gid}")->all();
		$this->assign("goodsAttrs",$goodsAttrs);
		//获得货品数据
		$goodsList = $this->db->where("vshop_goods_gid={$gid}")->limit($page->limit())->all();
		foreach ($goodsList as $k=>$v){
			$goodsList[$k]['combine'] = K("GoodsAttr")->field("gavalue")->where("gaid IN({$v['combine']})")->all();
		}
		$this->assign("glArr",$goodsList);
		$this->assign("pageData",$pageData);
		$this->display("goodsList");
	}
	/**
	 * 添加货品
	 */
	public function add(){
		if (IS_POST){
			if (!$this->db->addGoodsList()) $this->error($this->db->error);
			$gid = Q("post.gid",0,"intval");
			$inventory = Q("post.glinventory",0,"intval");
			M("goods")->inc("gallinventory","gid={$gid}",$inventory);
			$this->success("添加货品成功",U("index",array('gid'=>$gid)));
		}
	}
	/**
	 * 编辑货品
	 */
	public function edit() {
		if (IS_POST){
			$glid = Q("post.glid",0,"intval");
			//获得旧数据(所属商品ID、库存)
			$oldData = $this->db->field("glinventory,vshop_goods_gid")->where("glid={$glid}")->find();
			//处理商品表总库存
			$newInventory = Q("post.glinventory",0,"intval");
			$inventory = $newInventory - $oldData['glinventory'];
			if (!$this->db->editGoodsList()) $this->error($this->db->error);
			K("Goods")->inc("gallinventory","gid={$oldData['vshop_goods_gid']}",$inventory);
			$this->success("编辑货品成功",U("index",array("gid"=>$oldData['vshop_goods_gid'])));
		}
		$gid = Q("get.gid",0,"intval");
		$glid = Q("get.glid",0,"intval");
		//获得商品属性
		$tid = K("Goods")->field("vshop_type_tid")->where("gid={$gid}")->find();
		$attrsData = K("TypeAttr")->where("vshop_type_tid={$tid['vshop_type_tid']} AND tatype=1")->all();
		$this->assign("attrsData",$attrsData);
		$goodsAttrs = K("GoodsAttr")->where("vshop_goods_gid={$gid}")->all();
		$this->assign("goodsAttrs",$goodsAttrs);
		//获得货品数据
		$goodsList = $this->db->where("glid={$glid}")->find();
		$goodsList['combine'] = explode(",", $goodsList['combine']);
		$this->assign("goodsList",$goodsList);
		$this->display("editGoodsList");
	}
	/**
	 * 删除货品
	 */
	public function del() {
		$glid = Q("get.glid",0,"intval");
		if ($glid == 0) $this->error("货品不存在");
		//获得旧数据(所属商品ID、库存)
		$oldData = $this->db->field("glinventory,vshop_goods_gid")->where("glid={$glid}")->find();
		if ($this->db->where("glid={$glid}")->delete() != 1) $this->error("删除货品失败");
		//更新商品表总库存
		K("Goods")->dec("gallinventory","gid={$oldData['vshop_goods_gid']}",$oldData['glinventory']);
		$this->success("删除货品成功",U("index",array("gid"=>$oldData['vshop_goods_gid'])));
	}
}